package holdem;

import java.util.*;

/**
 *
 * @author Pai Qi
 * @author qipai0@gmail.com
 * Description:
 * Start Date:
 *
 */

public final class Player extends NestedIntegerList {
    //INVARIANCE: The order of fields (including static) should not be changed

    public enum PlayerEvent {fold, check, call, raise}


    public static List<Player> players;
    public static Integer      totalID;

    static {
	totalID = 0;
	players = new ArrayList<Player>();
    }

    //~--- fields & initializers ----------------------------------------------


    public Emotion emotion = new Emotion();
    public Integer id;
    public Integer tableLocation;
    public Boolean isFolded = false;
    public Integer moneyAtCurrent;
    public Integer moneyAtStarted;
    public String  playerName;
    public List<Card> handCards = new ArrayList<Card>();
    public Boolean handCardsAvailable = false;
    public Integer moneyBet = 0;
    public Boolean hasQuit = false;
    public Integer foldCount = 0;
    public Integer checkCount = 0;
    public Integer callCount = 0;
    public Integer raiseCount = 0;


    // 0 == fold , 1 == check, 2 == bet, 3 == raise in order of aggressiveniess
    public PlayerEvent lastEvent = PlayerEvent.check;

    //~--- constructors -------------------------------------------------------

    public Player(String name, int playerID, int moneyAtStarted) {
	playerName     = name;
        // we get player id from server now
	this.moneyAtStarted = moneyAtStarted;
        moneyAtCurrent = moneyAtStarted;
        tableLocation       = playerID;
        id = playerID;

        //System.out.println("NewPlayer" + name);
	// forbit sub classes
	players.add(this);

    }
    //~--- methods ------------------------------------------------------------

    @Override
    public boolean equals(Object o) {
	if (o instanceof Player) {
	    return ((Player) o).id == id;
	}

	return false;
    }

    @Override
    public String toString() {
	return "[Player"+id+"]";
    }

    public static List<Player> allPlayers() {
	return players;
    }

    public void betMoney(int amount) {
	assert amount > 0;
	this.moneyAtCurrent -= amount;
    }

    public void earnMoney(int amount) {
	assert amount > 0;
	this.moneyAtCurrent += amount;
    }

    public void fold() {
	isFolded = true;
    }

    public static Player me() {
	return null;
    }

    public Emotion getEmotion() {
	return emotion;
    }

    public List<Card> getHand() {
	return null;
    }

    public int getId() {
	return id;
    }

    public int getMoneyAtCurrent() {
	return moneyAtCurrent;
    }

    public int getMoneyAtStarted() {
	return moneyAtStarted;
    }

    @Override
    public String getName() {
	return playerName;
    }

    public static Player getPlayer(String name) {
	Iterator<Player> i = players.iterator();

	while (i.hasNext()) {
	    Player p = i.next();

	    if (name.equals(p.getName())) {
		return p;
	    }
	}

	return null;
    }

    public static Player getPlayer(int id) {
	return players.get(id-1);
    }

    // need further mod
    public boolean isFolded() {
	return isFolded;
    }

    public static int nextID() {
	return ++totalID;
    }
/*
    private Integer event
 *
 *
 * */
}
